package com.perfma.xtest.tool;

import com.google.common.collect.Lists;
import org.springframework.scheduling.support.CronSequenceGenerator;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author: junhui.si
 * @date: 2021-02-02 17:18
 **/
public class CronDemo {


    public static void main(String[] args) {
//        CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator("0 0 0 * * ?");
//        Date date = DateUtils.addDays(new Date(), -1);
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(date);
//        calendar.set(Calendar.HOUR_OF_DAY, 23);
//        calendar.set(Calendar.MINUTE, 59);
//        calendar.set(Calendar.SECOND, 59);
//        System.out.println(DateUtils.formatDateTime(calendar.getTime()));
//        System.out.println(DateUtils.formatDateTime(cronSequenceGenerator.next(calendar.getTime())));

        List<CronSequenceGenerator> list = Lists.newArrayList();
        for (int i = 0; i < 1000; i++) {
            int hour = i % 24;
            int min = i % 59;
//            String cron = "0 " + min + " " + hour + " * * ? ";
            int s = i % 59;
            String cron = s + " * * * * ? ";
            list.add(new CronSequenceGenerator(cron));
//            System.out.println(cron);
        }

        try{
            Thread.sleep(4000);
        }catch (Exception e){

        }
        List<String> dataList = Lists.newArrayList();
        int count = 0;
        long startTime = System.currentTimeMillis();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtils.addDays(new Date(), -1));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 0);
        Date date = calendar.getTime();
        Calendar calendar1 = Calendar.getInstance();
        calendar1.set(Calendar.HOUR_OF_DAY, 0);
        calendar1.set(Calendar.MINUTE, 0);
        calendar1.set(Calendar.SECOND, 0);
        calendar1.set(Calendar.MILLISECOND, 0);
        Date zero = calendar1.getTime();
        for (int i = 1; i < 31; i++) {
            for (CronSequenceGenerator cronSequenceGenerator : list) {
                Date temp = cronSequenceGenerator.next(date);
                if (temp.getTime() >= zero.getTime()) {
                    dataList.add(DateUtils.formatDateTime(temp));
                }
                count++;
            }
            date = DateUtils.addDays(date, 1);
            zero = DateUtils.addDays(zero, 1);
        }
//        System.out.println(dataList);

        System.out.println(System.currentTimeMillis() - startTime);
        System.out.println(count);
    }
}
